package com.yby6.framework.web.service;

import com.anji.captcha.service.CaptchaCacheService;
import com.yby6.common.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 安吉拉滑块自定义redis验证码缓存实现类
 *
 * @author yang shuai
 */
@Component
public class CaptchaRedisService implements CaptchaCacheService {
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Override
	public void set(String key, String value, long expiresInSeconds) {
		stringRedisTemplate.opsForValue().set(Constants.CAPTCHA_CODE_KEY + key, value, expiresInSeconds, TimeUnit.SECONDS);
	}

	@Override
	public boolean exists(String key) {
		return stringRedisTemplate.hasKey(Constants.CAPTCHA_CODE_KEY + key);
	}

	@Override
	public void delete(String key) {
		stringRedisTemplate.delete(Constants.CAPTCHA_CODE_KEY + key);
	}

	@Override
	public String get(String key) {
		return stringRedisTemplate.opsForValue().get(Constants.CAPTCHA_CODE_KEY + key);
	}

	@Override
	public Long increment(String key, long val) {
		return stringRedisTemplate.opsForValue().increment(Constants.CAPTCHA_CODE_KEY + key, val);
	}

	@Override
	public String type() {
		return "redis";
	}
}